package com.winchain.provider.expiration.controller;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.mvc.controller.BaseController;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.framework.utils.CommonFileUtil;
import com.winchain.chainbill.ChainBillUtil;
import com.winchain.chainbill.util.ExcelGenUtil;
import com.winchain.provider.expiration.service.CbCreditService;
import com.winchain.provider.expiration.service.CbDebitService;
import com.winchain.provider.expiration.vo.BatchStats;
import com.winchain.provider.expiration.vo.CbCreditQueryVo;
import com.winchain.provider.expiration.vo.CbCreditVo;
import com.winchain.provider.expiration.vo.CbDebitQueryVo;
import com.winchain.provider.expiration.vo.CbDebitVo;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ExpPaymentController extends BaseController {
	@Autowired
	private CbDebitService cbDebitService;
	@Autowired
	private CbCreditService cbCreditService;

	@RequestMapping
	@ResponseBody
	@Logging(module = "������������", methods = "����������������")
	public List<CbDebitVo> selectDebitList(PageLimit pageLimit, CbDebitQueryVo cbDebitQueryVo) {
		List<String> debitStates = new ArrayList();
		debitStates.add("0");
		debitStates.add("2");
		debitStates.add("3");
		cbDebitQueryVo.setDebitStates(debitStates);
		return this.cbDebitService.selectDebitList(pageLimit, cbDebitQueryVo);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "������������", methods = "��������")
	public RetMsg reDebit(String debitIds) throws Exception {
		if (StringUtil.emptyAndNull(debitIds)) {
			new RetMsg("error", false, "��������");
		}
		String[] debitIdArr = debitIds.split(",");
		BatchStats<String> stats = this.cbDebitService.reDebit(debitIdArr);
		String msg = null;
		String grpId = this.cbDebitService.getGrpIdByDebitId(debitIdArr[0]);
		if ("batch".equals(ChainBillUtil.getExpSettleMode(grpId))) {
			msg = "����";
		} else {
			msg = "����";
		}
		if (debitIdArr.length == stats.getAffectedRows()) {
			return new RetMsg("success", true, "����" + msg + "����", stats.getTxSnHost());
		}
		if (stats.getAffectedRows() == 0) {
			return new RetMsg("error", false, "����" + msg + "����", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
		}
		return new RetMsg("warning", false, "����" + msg + "��������", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "����������")
	public RetMsg disburse(String debitIds) throws IOException {
		if (StringUtil.emptyAndNull(debitIds)) {
			new RetMsg("error", false, "��������");
		}
		String[] debitIdArr = debitIds.split(",");
		BatchStats<String> stats = this.cbDebitService.disburse(debitIdArr);
		if (debitIdArr.length == stats.getAffectedRows()) {
			return new RetMsg("success", true, "������������", stats.getTxSnHost());
		}
		if (stats.getAffectedRows() == 0) {
			return new RetMsg("error", false, "������������", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
		}
		return new RetMsg("warning", false, "����������������", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������")
	public RetMsg offlineProcess(String debitIds) throws IOException {
		if (StringUtil.emptyAndNull(debitIds)) {
			new RetMsg("error", false, "��������");
		}
		String[] debitIdArr = debitIds.split(",");
		BatchStats<String> stats = this.cbDebitService.offlineProcess(debitIdArr);
		if (debitIdArr.length == stats.getAffectedRows()) {
			return new RetMsg("success", true, "������������");
		}
		if (stats.getAffectedRows() == 0) {
			return new RetMsg("error", false, "������������", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
		}
		return new RetMsg("warning", false, "����������������", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������������������")
	public List<CbDebitVo> selectOverdueDebitList(PageLimit pageLimit, CbDebitQueryVo cbDebitQueryVo) {
		cbDebitQueryVo.setDebitState("5");
		return this.cbDebitService.selectDebitList(pageLimit, cbDebitQueryVo);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������������������")
	public Map<String, String> getCashMap(String debitId) {
		return this.cbDebitService.getCashMap(debitId);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������")
	public RetMsg confirmDebit(String debitIds, String cashId, String settleDate, BigDecimal actualAmount,
			BigDecimal overdueIns, String remark) throws IOException {
		if (StringUtil.emptyAndNull(debitIds)) {
			new RetMsg("error", false, "��������");
		}
		String[] debitIdArr = debitIds.split(",");
		BatchStats<String> stats = this.cbDebitService.confirmDebit(debitIdArr, cashId, settleDate, actualAmount,
				overdueIns, remark);
		if (debitIdArr.length == stats.getAffectedRows()) {
			return new RetMsg("success", true, "��������", stats.getTxSnHost());
		}
		if (stats.getAffectedRows() == 0) {
			return new RetMsg("error", false, "��������", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
		}
		return new RetMsg("warning", false, "������������", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "������������", methods = "��������")
	public RetMsg reCredit(String creditIds) throws Exception {
		if (StringUtil.emptyAndNull(creditIds)) {
			new RetMsg("error", false, "��������");
		}
		String[] creditIdArr = creditIds.split(",");
		BatchStats<String> stats = this.cbCreditService.reCredit(creditIdArr);
		if (creditIdArr.length == stats.getAffectedRows()) {
			return new RetMsg("success", true, "������������", stats.getTxSnHost());
		}
		if (stats.getAffectedRows() == 0) {
			return new RetMsg("error", false, "������������", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
		}
		return new RetMsg("warning", false, "����������������", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "����������������")
	public List<CbCreditVo> selectCreditList(PageLimit pageLimit, CbCreditQueryVo cbCreditQueryVo) {
		List<String> creditStates = new ArrayList();
		creditStates.add("0");
		creditStates.add("2");
		creditStates.add("3");
		cbCreditQueryVo.setCreditStates(creditStates);
		pageLimit.setSortName("edit_time");
		pageLimit.setSortType("DESC");
		return this.cbCreditService.selectCreditList(pageLimit, cbCreditQueryVo);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������")
	public RetMsg confirmCredit(String creditIds) throws IOException {
		String[] creditIdArr = creditIds.split(",");
		BatchStats<String> stats = this.cbCreditService.confirmCredit(creditIdArr);
		if (creditIdArr.length == stats.getAffectedRows()) {
			return new RetMsg("success", true, "������������", stats.getTxSnHost());
		}
		if (stats.getAffectedRows() == 0) {
			return new RetMsg("error", true, "������������", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
		}
		return new RetMsg("warning", true, "����������������", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "������������")
	public void exportCreditList(PageLimit pageLimit, CbCreditQueryVo cbCreditQueryVo, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<String> creditStates = new ArrayList();
		creditStates.add("0");
		creditStates.add("2");
		creditStates.add("3");
		cbCreditQueryVo.setCreditStates(creditStates);
		pageLimit.setSortName("edit_time");
		pageLimit.setSortType("DESC");
		Map<String, Object> map = new HashMap();
		map.put("datas", this.cbCreditService.selectCreditList(pageLimit, cbCreditQueryVo));
		String execelTemp = "/template/excel/creditReportList.xlsx";
		String fileName = "������������";

		map.put("listNm", fileName);
		InputStream is = ExcelGenUtil.getInstance().GenerateExcelForStream(execelTemp, map);
		CommonFileUtil.writeFileToClient(fileName + ".xlsx", is, response, request);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "����������������")
	public String getExpSettleMode(String grpId) {
		return ChainBillUtil.getExpSettleMode(grpId);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "����")
	public List<CbDebitVo> selectAllDebitList(PageLimit limit, CbDebitQueryVo cbDebitQueryVo) {
		List<CbDebitVo> list = this.cbDebitService.selectDebitCashList(limit, cbDebitQueryVo);
		return list;
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������")
	public List<CbDebitVo> selectCashDebitList(PageLimit limit, CbDebitQueryVo cbDebitQueryVo) {
		List<String> debitStates = new ArrayList();
		debitStates.add("0");
		debitStates.add("2");
		debitStates.add("3");
		cbDebitQueryVo.setDebitStates(debitStates);
		return this.cbDebitService.selectDebitCashList(limit, cbDebitQueryVo);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������")
	public List<CbCreditVo> selectDebitCreditList(PageLimit limit, CbCreditQueryVo cbCreditQueryVo) {
		List<String> creditTypes = new ArrayList();
		creditTypes.add("0");
		creditTypes.add("2");
		cbCreditQueryVo.setCreditTypes(creditTypes);
		return this.cbCreditService.selectDebitCreditList(limit, cbCreditQueryVo);
	}
}
